using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;
using System.IO;

public partial class Admin_CapNhatSanPham : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDropDownList();
            LoadData();
        }
    }
            private void LoadDropDownList()
    {


        tbLoaiSanPhamEntities.tbLoaiSanPhamEntitiesCollection LoaiSPCollection = tbLoaiSanPhamBUS.GetAll();
        ddl_LoaiSP.DataTextField = tbLoaiSanPhamEntities.tbLoaiSanPhanEntitiesColumns.TenLoai.ToString();
        ddl_LoaiSP.DataValueField = tbLoaiSanPhamEntities.tbLoaiSanPhanEntitiesColumns.MaLoai.ToString();
        ddl_LoaiSP.DataSource = LoaiSPCollection;
        ddl_LoaiSP.DataBind();
       
    }
    private void LoadData()
    {
   try
        {
            int id = int.Parse(Request.QueryString["id"]);

            tbSanPhamEntities sanPham = tbSanPhamBUS.GetByID(id);
            txt_MaSP.Text = sanPham.MaSP.ToString();
            txt_TenSP.Text = sanPham.TenSP;           
            txt_GiaGoc.Text = sanPham.GiaGoc.ToString();
            txt_GiaBan.Text = sanPham.GiaBan.ToString();
            txt_MoTa.Text = sanPham.MoTa;   
            txt_SoLuong.Text = sanPham.SLCon.ToString();            
           ddl_LoaiSP.SelectedValue = sanPham.MaLoai.ToString();
           int Total = 0;
           Criteria Cri = new Criteria();
           Cri.add(Condition.AND, tbHinhAnhEntities.tbHinhAnhEntitiesColumns.MaSP, Condition.EQUAL, int.Parse(txt_MaSP.Text));
           tbHinhAnhEntities.tbHinhAnhEntitiesCollection hinhAnhCollection = tbHinhAnhBUS.Get(Cri.Criter, PagerV2_8_1.CurrentIndex, PagerV2_8_1.PageSize,
                                                                    tbHinhAnhEntities.tbHinhAnhEntitiesColumns.STT.ToString(),
                                                                    OrderDirection.ASC.ToString(), out Total);
           Repeater1.DataSource = hinhAnhCollection;
           Repeater1.DataBind();
           PagerV2_8_1.ItemCount = Total;
           if (PagerV2_8_1.ItemCount <= PagerV2_8_1.PageSize)
               PagerV2_8_1.Visible = false;
           else
               PagerV2_8_1.Visible = true;
          

        }
        catch
        {
            Response.Redirect("~/Admin/DanhSachSanPham.aspx");
        }

    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        try
        {

            int id = int.Parse(Request.QueryString["id"].ToString());
            tbSanPhamEntities sanPham = tbSanPhamBUS.GetByID(id);
            sanPham.TenSP = txt_TenSP.Text;
            sanPham.MoTa = txt_MoTa.Text;
            sanPham.MaLoai = int.Parse(ddl_LoaiSP.SelectedValue);
            sanPham.SLCon = int.Parse(txt_SoLuong.Text);
            sanPham.GiaGoc = int.Parse(txt_GiaGoc.Text);
            sanPham.GiaBan = int.Parse(txt_GiaBan.Text);
            sanPham.NgayCapNhat = DateTime.Now.Date;
            tbSanPhamBUS.Update(sanPham);
             

             HttpFileCollection hfc = Request.Files;
             for (int i = 0; i < hfc.Count; i++)
             {
                 HttpPostedFile hpf = hfc[i];
                 if (hpf.ContentLength > 0)
                 {
                     //hpf.SaveAs(Server.MapPath("Photos") + "_" + Path.GetFileName(hpf.FileName));
                     string filename = DateTime.Now.Ticks.ToString() + "_" + Path.GetFileName(hpf.FileName);
                     hpf.SaveAs(Server.MapPath("~/Photos/" + filename));

                     System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/Photos/" + filename));
                     ImageUtility.MakeThumbnailPhoto(img, 100, 100, Server.MapPath("~/Photos/Thumb/"), filename);

                     tbHinhAnhEntities ha = new tbHinhAnhEntities();
                     ha.MaSP = id;
                     ha.HinhAnh = filename;
                     tbHinhAnhBUS.Insert(ha);

                 }
             }
             Response.Redirect("~/Admin/DanhSachSanPham.aspx");
        }
        catch
        {
             Response.Redirect("~/Admin/DanhSachSanPham.aspx");
        }
     Response.Redirect(Request.RawUrl);
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.Item)
        {
            tbHinhAnhEntities hinhAnh = (tbHinhAnhEntities)e.Item.DataItem;
            Literal lit_IdHinh = (Literal)e.Item.FindControl("lit_IdHinh");
            HiddenField hf_id = (HiddenField)e.Item.FindControl("hf_id");
            HiddenField hf_hinh = (HiddenField)e.Item.FindControl("hf_tenhinh");           
            Image img_HinhAnh = (Image)e.Item.FindControl("img_HinhAnh");

            lit_IdHinh.Text = hinhAnh.STT.ToString();
            hf_id.Value = lit_IdHinh.Text;
            hf_hinh.Value = hinhAnh.HinhAnh.ToString();
            img_HinhAnh.ImageUrl = "~/Photos/Thumb/" + hinhAnh.HinhAnh.ToString();
        }
    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        PagerV2_8_1.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();
    }
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.Repeater1.Items)
        {
            CheckBox cb_Xoa = (CheckBox)item.FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_hinh = (HiddenField)item.FindControl("hf_tenhinh");
                File.Delete(Server.MapPath("~/Photos/") + hf_hinh.Value);
                File.Delete(Server.MapPath("~/Photos/Thumb/") + hf_hinh.Value);

                HiddenField hf_id = (HiddenField)item.FindControl("hf_id");                
                tbHinhAnhBUS.Delete(int.Parse(hf_id.Value));
            }

        }
        LoadData();

        Response.Redirect(Request.RawUrl);
    }
}
